//
//  DSZTJCollectionViewCell.m
//  7.13苏宁框架
//
//  Created by app15 on 15/7/16.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZTJCollectionViewCell.h"
#import "DSZTJTableViewCell.h"
#import "DSZTJShowModel.h"
#import "DSZTJCommentModel.h"
#import "MJExtension.h"
#import "UIImageView+WebCache.h"
#import "DSZTJHeaderView.h"
#import "DSZTJXQViewController.h"
#import "DSZTJGroupModel.h"
#import "DSZGZXQViewController.h"

NSInteger commentNum;

@interface DSZTJCollectionViewCell()
{
    DSZTJShowModel *TJshowModel;
    DSZTJGroupModel *TJgroupModel;
}
@property(nonatomic,strong)NSArray *commentArray;
@property(nonatomic,strong)NSArray *array;
@end
@implementation DSZTJCollectionViewCell
-(void)awakeFromNib{
    _attentionBtn.layer.borderWidth = 1;
    _attentionBtn.layer.borderColor = [UIColor orangeColor].CGColor;
    _attentionBtn.layer.cornerRadius = 5;
    _attentionBtn.layer.masksToBounds = YES;
}
+(NSString*)xib{
    return @"DSZTJCollectionViewCell";
}
//-(instancetype)init{
//    self=[super init];
//    if (self) {
//        self=[[[NSBundle mainBundle]loadNibNamed:@"DSZTJCollectionViewCell" owner:nil options:nil]lastObject];
//    }
//    return self;
//}

-(void)modelTable:(DSZTJShowModel*)model group:(DSZTJGroupModel*)goupmodel{
    TJgroupModel=goupmodel;
    TJshowModel=model;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    //tap.numberOfTapsRequired = 2;
    [self.contentImageView addGestureRecognizer:tap];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)];
    //tap.numberOfTapsRequired = 2;
    [self.NextViewXq addGestureRecognizer:tap1];
    
    NSDictionary *dic=[model keyValues];
    NSArray *array=dic[@"comment"];
    NSArray *commentarray=[DSZTJCommentModel modelArrayWithDictArray:array];
    commentNum=commentarray.count;
    self.commentArray=commentarray;
    [self.tableView reloadData];
   // return
}

- (void)tap:(UITapGestureRecognizer *)tapGetrue {
    DSZTJXQViewController *view=[[DSZTJXQViewController alloc]init];
    [view Drgroupmode:TJgroupModel show:TJshowModel];
    if ([self.delegate respondsToSelector:@selector(TJXQview)]) {
        [_delegate TJXQview];
    }
}

- (void)tap1:(UITapGestureRecognizer *)tapGetrue {
    DSZGZXQViewController *view=[[DSZGZXQViewController alloc]init];
    [view XqGroupModel:TJgroupModel];
   // [view Drgroupmode:TJgroupModel show:TJshowModel];
    if ([self.delegate respondsToSelector:@selector(GZXQview)]) {
        [_delegate GZXQview];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (commentNum<=3) {
        return commentNum;
    }else{
        return 3;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *iden=@"cell";
    DSZTJTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:iden];
    DSZTJCommentModel *model= _commentArray[indexPath.row];
    if (cell==nil) {
        cell=[[DSZTJTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    tableView.separatorStyle=0;
     if (indexPath.row==0)
    {
        cell.commentLabel.hidden=NO;
    }
    if (indexPath.row==(commentNum-1)&&commentNum<=3) {
        cell.commentLineView.hidden=YES;
    }
    [cell.commentImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.image]]];
    
    NSString *str=model.name;
    CGFloat i=str.length;

    CGRect len=cell.commentNameLabel.frame;
    CGRect len1=cell.commentContentlabel.frame;
    len=CGRectMake(cell.commentNameLabel.frame.origin.x, cell.commentNameLabel.frame.origin.y, 10+i*15, cell.commentNameLabel.frame.size.height);
    len1=CGRectMake(cell.commentNameLabel.frame.origin.x+i*15+10, cell.commentContentlabel.frame.origin.y,cell.commentContentlabel.frame.size.width-i*15, cell.commentContentlabel.frame.size.height);
    cell.commentContentlabel.frame=len1;
    cell.commentNameLabel.frame=len;

    cell.commentNameLabel.text=[NSString stringWithFormat:@"%@:",model.name];
    [cell.commentNumBtn setTitle:[NSString stringWithFormat:@"%i",commentNum] forState:UIControlStateNormal];
    cell.selectionStyle=0;
    cell.commentContentlabel.text=model.content;
    return cell;
}
//自定义断尾
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(commentNum<=3){
        return 0;
    }else{
    UIView *view=[[UIView alloc]init];
    UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 8, 310, 30)];
    lable.textAlignment=NSTextAlignmentRight;
    lable.text=[NSString stringWithFormat:@"查看所有%i条评论",commentNum];
    lable.font=[UIFont systemFontOfSize:15];
    lable.textColor=[UIColor orangeColor];
    [view addSubview:lable];
        //lable.hidden=YES;
    tableView.tableFooterView=view;
    return view;
    }
    
}
//自定义段头
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    DSZTJHeaderView *view=[[DSZTJHeaderView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
- (IBAction)commentBtn {
    if ([self.delegate respondsToSelector:@selector(comment)]) {
        [_delegate comment];
    }
}

- (IBAction)shareBtn {
    if ([self.delegate respondsToSelector:@selector(share)]) {
        [_delegate share];
    }
}
@end
